package com.bookocean.controller;

import com.bookocean.application.common.Result;
import com.bookocean.application.service.InteractionApplicationService;
import com.bookocean.controller.dto.CommentDto;
import com.bookocean.controller.dto.FavoriteDto;
import com.bookocean.controller.dto.PageResultDto;
import com.bookocean.domain.utils.UserContextHolder;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * 通用互动控制器（点赞、评论、收藏）
 * 支持书摘、小说、章节等多种内容类型
 * 
 * @author BookOcean
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/interaction")
@RequiredArgsConstructor
public class InteractionController {

    private final InteractionApplicationService interactionApplicationService;

    /**
     * 点赞
     * 
     * @param targetType 目标类型：1-书摘，2-小说，3-章节
     * @param targetId 目标ID
     * @return 操作结果
     */
    @PostMapping("/like")
    public Result<String> like(@RequestParam Integer targetType, @RequestParam Long targetId) {
        Long currentUserId = UserContextHolder.getUserId();
        interactionApplicationService.like(targetId, targetType, currentUserId);
        return Result.success("点赞成功");
    }

    /**
     * 取消点赞
     * 
     * @param targetType 目标类型：1-书摘，2-小说，3-章节
     * @param targetId 目标ID
     * @return 操作结果
     */
    @PostMapping("/unlike")
    public Result<String> unlike(@RequestParam Integer targetType, @RequestParam Long targetId) {
        Long currentUserId = UserContextHolder.getUserId();
        interactionApplicationService.unlike(targetId, targetType, currentUserId);
        return Result.success("取消点赞成功");
    }

    /**
     * 收藏
     * 
     * @param targetType 目标类型：1-书摘，2-小说，3-章节
     * @param targetId 目标ID
     * @return 操作结果
     */
    @PostMapping("/favorite")
    public Result<String> favorite(@RequestParam Integer targetType, @RequestParam Long targetId) {
        Long currentUserId = UserContextHolder.getUserId();
        interactionApplicationService.favorite(targetId, targetType, currentUserId);
        return Result.success("收藏成功");
    }

    /**
     * 取消收藏
     * 
     * @param targetType 目标类型：1-书摘，2-小说，3-章节
     * @param targetId 目标ID
     * @return 操作结果
     */
    @PostMapping("/unfavorite")
    public Result<String> unfavorite(@RequestParam Integer targetType, @RequestParam Long targetId) {
        Long currentUserId = UserContextHolder.getUserId();
        interactionApplicationService.unfavorite(targetId, targetType, currentUserId);
        return Result.success("取消收藏成功");
    }

    /**
     * 获取评论列表
     * 
     * @param targetType 目标类型：1-书摘，2-小说，3-章节
     * @param targetId 目标ID
     * @param page 页码
     * @param pageSize 每页数量
     * @return 分页评论列表
     */
    @GetMapping("/comments")
    public Result<PageResultDto<CommentDto>> getComments(
            @RequestParam Integer targetType,
            @RequestParam Long targetId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        PageResultDto<CommentDto> result = interactionApplicationService.getComments(
            targetId, targetType, page, pageSize);
        return Result.success(result);
    }

    /**
     * 添加评论
     * 
     * @param targetType 目标类型：1-书摘，2-小说，3-章节
     * @param targetId 目标ID
     * @param commentData 评论数据
     * @return 操作结果
     */
    @PostMapping("/comment")
    public Result<String> comment(
            @RequestParam Integer targetType,
            @RequestParam Long targetId,
            @Valid @RequestBody CommentData commentData) {

        Long currentUserId = UserContextHolder.getUserId();
        interactionApplicationService.comment(targetId, targetType, currentUserId, 
                commentData.getContent(), commentData.getParentId());
        return Result.success("评论成功");
    }

    /**
     * 回复评论
     * 
     * @param commentId 评论ID
     * @param replyData 回复数据
     * @return 操作结果
     */
    @PostMapping("/reply/{commentId}")
    public Result<String> replyComment(
            @PathVariable Long commentId,
            @Valid @RequestBody CommentData replyData) {

        Long currentUserId = UserContextHolder.getUserId();
        interactionApplicationService.replyComment(commentId, currentUserId, replyData.getContent());
        return Result.success("回复成功");
    }

    /**
     * 获取用户收藏列表
     * 
     * @param targetType 目标类型：1-书摘，2-小说，3-章节（可选，不传则返回所有类型）
     * @param page 页码
     * @param pageSize 每页数量
     * @return 分页收藏列表
     */
    @GetMapping("/favorites")
    public Result<PageResultDto<FavoriteDto>> getUserFavorites(
            @RequestParam(required = false) Integer targetType,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        
        Long currentUserId = UserContextHolder.getUserId();
        // 默认只返回书摘类型的收藏
        Integer filterType = targetType != null ? targetType : 1;
        PageResultDto<FavoriteDto> result = interactionApplicationService.getUserFavorites(
            currentUserId, filterType, page, pageSize);
        return Result.success(result);
    }


    /**
     * 评论数据内部类
     */
    public static class CommentData {
        private String content;
        private Long parentId;

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public Long getParentId() {
            return parentId;
        }

        public void setParentId(Long parentId) {
            this.parentId = parentId;
        }
    }
}
